import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  return {
    base: mode === 'development' ? '/' : '/web/',
    plugins: [vue()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    server: {
      port: 3000,
      host: '0.0.0.0', // 允许外部访问
      proxy: {
        // 代理 /api 请求到后端服务器
        '/api': {
          target: 'https://app.wdyky.com', // 后端服务器地址
          changeOrigin: true, // 改变源
          rewrite: (path) => path.replace(/^\/api/, ''), // 重写路径，去掉 /api 前缀
          secure: false, // 如果后端使用 HTTPS，需要设置为 false
          ws: true, // 代理 websockets
          configure: (proxy, _options) => {
            proxy.on('error', (err, _req, _res) => {
              console.log('proxy error', err)
            })
            //proxy.on('proxyReq', (proxyReq, req, _res) => {
            //proxy.on('proxyReq', (proxyReq, req, _res) => {
              //console.log('Sending Request to the Target:', req.method, req.url)
            //})
            //proxy.on('proxyRes', (proxyRes, req, _res) => {
              //console.log('Received Response from the Target:', proxyRes.statusCode, req.url)
            //})
          }
        },
        '/upload': {
          target: 'https://app.wdyky.com',
          changeOrigin: true,
          secure: false
        }
      },
      cors: true // 默认启用 CORS
    },
    build: {
      target: 'es2015',
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: false,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    },
    envPrefix: 'VITE_'
  }
})